<template>
  <div class="login-page">
    <div class="content">
      <div class="center-content">
        <img src="@/assets/img/Woolf.jpg" alt="Virginia Woolf" class="avatar">
        <h2>Welcome Back</h2>
        <form @submit.prevent="handleLogin" class="login-form">
          <div class="form-group">
            <input 
              type="text" 
              id="userId" 
              v-model="userId" 
              required
              placeholder="用户ID"
            >
          </div>
          <div class="form-group">
            <input 
              type="password" 
              id="password" 
              v-model="password" 
              required
              placeholder="密码"
            >
          </div>
          <div class="form-group captcha-group">
            <input 
              type="text" 
              id="captcha" 
              v-model="captcha" 
              required
              placeholder="验证码"
            >
            <div class="captcha-code" @click="refreshCaptcha">
              {{ captchaCode }}
            </div>
          </div>
          <button type="submit" class="submit-btn">登陆</button>
          <router-link to="/register" class="back-btn">注册</router-link>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      userId: '',
      password: '',
      captcha: '',
      captchaCode: ''
    }
  },
  created() {
    this.refreshCaptcha()
  },
  methods: {
    refreshCaptcha() {
      const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
      let code = ''
      for (let i = 0; i < 4; i++) {
        code += chars.charAt(Math.floor(Math.random() * chars.length))
      }
      this.captchaCode = code
    },
    async handleLogin() {
      if (this.captcha.toLowerCase() !== this.captchaCode.toLowerCase()) {
        alert('验证码错误')
        this.refreshCaptcha()
        return
      }

      try {
        const response = await this.$axios.post('/api/login', {
          userId: this.userId,
          password: this.password
        })

        localStorage.setItem('token', response.data.token)
        localStorage.setItem('userId', response.data.user.id)
        localStorage.setItem('userRole', response.data.user.role)
        const redirect = this.$route.query.redirect || { name: 'Home' }
        this.$router.push(redirect)
      } catch (error) {
        alert('登陆失败' + (error.response?.data?.message || '未知错误'))
        this.refreshCaptcha()
      }
    }
  }
}
</script>

<style scoped>
.login-page {
  width: 100vw;
  height: 100vh;
  background-image: url('~@/assets/img/bg.jpg');
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.login-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

.content {
  position: relative;
  z-index: 1;
  text-align: center;
  padding: 2rem;
  background: rgba(47, 79, 79, 0.6);
  border-radius: 10px;
  backdrop-filter: blur(10px);
  max-width: 400px;
  width: 90%;
  margin: 0 auto;
}

.center-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 1.5rem;
  border: 2px solid rgba(255, 255, 255, 0.8);
}

h2 {
  color: white;
  font-size: 2rem;
  margin-bottom: 2rem;
  font-family: 'Century Gothic', sans-serif;
  letter-spacing: 2px;
}

.login-form {
  width: 100%;
  max-width: 300px;
}

.form-group {
  margin-bottom: 1.5rem;
}

input {
  width: 100%;
  padding: 0.8rem 1rem;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 25px;
  color: white;
  font-size: 1rem;
  transition: all 0.3s ease;
}

input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

input:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
}

.captcha-group {
  display: flex;
  gap: 1rem;
}

.captcha-code {
  background: rgba(255, 255, 255, 0.1);
  padding: 0.8rem 1rem;
  border-radius: 25px;
  color: white;
  cursor: pointer;
  user-select: none;
  min-width: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: monospace;
  letter-spacing: 2px;
}

.submit-btn {
  width: 100%;
  padding: 0.8rem;
  background: transparent;
  border: 2px solid rgba(255, 255, 255, 0.8);
  color: white;
  border-radius: 25px;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 1rem;
}

.submit-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.back-btn {
  display: inline-block;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.back-btn:hover {
  color: white;
}
</style>